package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.mapper.RoleMapper;
import cn.tedu.oa_system.mapper.SysMenuMapper;
import cn.tedu.oa_system.pojo.Role;
import cn.tedu.oa_system.pojo.SysMenu;
import cn.tedu.oa_system.pojo.in.FindLikeKeyIn;
import cn.tedu.oa_system.pojo.in.PageIn;
import cn.tedu.oa_system.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author MW
 * @Description 角色服务实现类
 * @CreateDate 2023/3/5
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public ResultDto<Void> addRole(Role role) {
        roleMapper.insert(role);
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> deleteRole(Long roleId) {
        roleMapper.deleteById(roleId);
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> updateRole(Role role) {
        roleMapper.updateById(role);
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDataDto<Role> findByLikeKey(FindLikeKeyIn likeKeyIn) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        String likeKey = likeKeyIn.getLikeKey();
        queryWrapper.like(Role::getRoleName,likeKey).or()
                .like(Role::getRoleValue,likeKey);
        ResultDataDto<Role> resultDataDto = new ResultDataDto<>();
        Page<Role> page = new Page(likeKeyIn.getCurrent(), likeKeyIn.getSize());
        Page<Role> userPage = roleMapper.selectPage(page, queryWrapper);
        List<Role> records = userPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDataDto<Role> roleList(PageIn pageIn) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Role::getRoleId,-1);
        ResultDataDto<Role> resultDataDto = new ResultDataDto<>();
        Page<Role> page = new Page(pageIn.getCurrent(), pageIn.getSize());
        Page<Role> userPage = roleMapper.selectPage(page, queryWrapper);
        List<Role> records = userPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDataDto<SysMenu> permissionsList(Long roleId) {
        List<SysMenu> parentMenuListByRoleId = sysMenuMapper.getParentMenuListByRoleId(roleId);
        log.debug("已获取到父菜单");
        List<SysMenu> sonMenuListByRoleId = sysMenuMapper.getSonMenuListByRoleId(roleId);
        log.debug("已获取到子菜单");
        List<SysMenu> menuList=new ArrayList<>();
        for (int i = 0; i < parentMenuListByRoleId.size()-1; i++) {
            menuList.add(parentMenuListByRoleId.get(i));
            for (int j = 0; j < sonMenuListByRoleId.size()-1; j++) {
                if (sonMenuListByRoleId.get(j).getParentId().equals(parentMenuListByRoleId.get(i).getMenuId())){
                    menuList.add(sonMenuListByRoleId.get(j));
                }
            }
        }
        ResultDataDto<SysMenu> resultDataDto = new ResultDataDto<>();
        resultDataDto.setDataList(menuList);
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }
}
